//
//  KDPostNotyController.m
//  WangJingApp
//
//  Created by 魏强 on 17/2/6.
//  Copyright © 2017年 KDTech. All rights reserved.
//

#import "KDPostNotyController.h"
#import "IQTextView.h"
#import "ZZPhotoKit.h"
#import "KDPicsCell.h"
#import "KDSecondNotyController.h"
@interface KDPostNotyController ()<UICollectionViewDataSource,UICollectionViewDelegate>
/**  通知内容的textView */
@property (weak, nonatomic) IBOutlet IQTextView *contentTv;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scroll_cons_h;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scroll_cons_w;
/**  scrollView的兜底View */
@property (weak, nonatomic) IBOutlet UIView *underView;

/**  所有想要添加的图片 包括那两张按钮图片*/
@property(strong  ,nonatomic) NSArray *picArray;
/**  图片集成展示的collectionView  */
@property(strong,nonatomic) UICollectionView *collectionView;

@end

@implementation KDPostNotyController
#pragma mark - 懒加载区
- (NSArray *)picArray
{
    if (!_picArray) {
        UIImage *image = [UIImage imageNamed:@"添加照片"];
//        PHImageManager *manager = [PHImageManager defaultManager];
//        
//        ZZPhoto *photo = [ZZPhoto ]
        _picArray = @[image,image] ;
//        KDLog(@"%lu", (unsigned long)_picArray.count);
    }
    return _picArray;
}
#pragma mark - 生命周期区
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BackGroundColor;
    self.title = @"发通知";
//    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setupContentTv];
    [self setupCollectionView];
    [self setupNextBtn];
    // Do any additional setup after loading the view from its nib.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.scroll_cons_w.constant = screenW;
    self.scroll_cons_h.constant = screenH - 64;
}
#pragma mark - 板块布置区
- (void)setupContentTv{

    self.contentTv.placeholder = @"请输入通知内容";
    self.contentTv.layer.cornerRadius = 10;
    self.contentTv.layer.borderWidth = 1;
    self.contentTv.layer.borderColor = WQColor(234, 234, 234).CGColor;
    
}

- (void)setupCollectionView{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    CGFloat photoSize = (screenW - 50) / 3;
    flowLayout.minimumInteritemSpacing = 10.0;//item 之间的行的距离
    flowLayout.minimumLineSpacing = 10.0;//item 之间竖的距离
    flowLayout.itemSize = (CGSize){photoSize,photoSize};
    //        self.sectionInset = UIEdgeInsetsMake(0, 2, 0, 0);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    [self.underView addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentTv.left);
        make.right.mas_equalTo(self.contentTv.mas_right);
        make.top.mas_equalTo(self.contentTv.mas_bottom).offset(10);
        make.height.equalTo(@(photoSize * 2 + 10));
    }];
    [_collectionView registerClass:[KDPicsCell class] forCellWithReuseIdentifier:@"KDPicsCell"];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView setUserInteractionEnabled:YES];
    
}

- (void)setupNextBtn{
    UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.underView addSubview:nextBtn];
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.underView.mas_left).offset(10);
        make.top.mas_equalTo(self.collectionView.mas_bottom).offset(10);
        make.centerX.equalTo(self.underView);
        make.height.equalTo(@44);
    }];
    
    
//    nextBtn.frame = CGRectMake(0, CGRectGetMaxY(regionLb.frame), screenW, 44);
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextBtn addTarget:self action:@selector(nextBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    nextBtn.backgroundColor = KDTintColor;
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];;
//    self.<#buttonName#> = <#buttonName#>;
}
#pragma mark - 事件点击区
- (void)nextBtnClick:(UIButton *)nextBtn{
    KDSecondNotyController *second = [[KDSecondNotyController alloc]init];
    [self.navigationController pushViewController:second animated:YES];
}
#pragma mark - 网络请求区

#pragma mark - 通知接收区
#pragma mark --- ZZBrowserPickerDelegate
-(NSInteger)zzbrowserPickerPhotoNum:(ZZBrowserPickerViewController *)controller
{
    return 5;
}

-(NSArray *)zzbrowserPickerPhotoContent:(ZZBrowserPickerViewController *)controller
{
    NSArray *array = @[
                       @"http://pic86.nipic.com/file/20151229/11592367_090842563000_2.jpg",
                       [UIImage imageNamed:@"scv2.jpg"],
                       [UIImage imageNamed:@"scv3.jpg"],
                       [UIImage imageNamed:@"scv4.jpg"],
                       [UIImage imageNamed:@"scv5.jpg"],
                       ];
    return array;
}

#pragma mark - UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    KDLog(@"%lu", (unsigned long)self.picArray.count);

    return self.picArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    KDPicsCell *photoCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"KDPicsCell" forIndexPath:indexPath];
    if (!photoCell) {
        photoCell = [[KDPicsCell alloc]init];
    }
    if ([[self.picArray objectAtIndex:indexPath.row] isKindOfClass:[ZZPhoto class]]) {
        
        //从数组里面把model取出来
        ZZPhoto *photo = [self.picArray objectAtIndex:indexPath.row];
        
        //用photo对象中的originImage属性来展示图片
        photoCell.photo.image = photo.originImage;
        
        
    }else if([[self.picArray objectAtIndex:indexPath.row] isKindOfClass:[ZZCamera class]]){
        
        ZZCamera *camera = [self.picArray objectAtIndex:indexPath.row];
        photoCell.photo.image = camera.image;
        
    }else if ([[self.picArray objectAtIndex:indexPath.row] isKindOfClass:[UIImage class]]){
        photoCell.photo.image = self.picArray[indexPath.row];
    }
    
    
    return photoCell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.picArray.count-1) {
        //点中了倒数第一个，就让他去相册找
        
        ZZPhotoController *photoController = [[ZZPhotoController alloc]init];
        photoController.selectPhotoOfMax = 8;
        //设置相册中完成按钮旁边小圆点颜色。
        //        photoController.roundColor = [UIColor greenColor];
        
        [photoController showIn:self result:^(id responseObject){
            NSMutableArray *arr = [NSMutableArray arrayWithArray:self.picArray];
            NSArray *photos = (NSArray *)responseObject;
            [arr insertObjects:photos atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, photos.count)]];
            self.picArray = arr;
            //            NSLog(@"重载");
            [_collectionView reloadData];
            
        }];
    }else if (indexPath.row == self.picArray.count-2){
         //点中了倒数第二个，就让他去相机找
        
        ZZCameraController *cameraController = [[ZZCameraController alloc]init];
        cameraController.takePhotoOfMax = 8;
        
        cameraController.isSaveLocal = NO;
        [cameraController showIn:self result:^(id responseObject){
            
            NSMutableArray *arr = [NSMutableArray arrayWithArray:self.picArray];
            NSArray *photos = (NSArray *)responseObject;
            [arr insertObjects:photos atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, photos.count)]];

            self.picArray = arr;
            
            
            [_collectionView reloadData];
        }];
    }else{//其他的都是删除
        NSMutableArray *arr = [NSMutableArray arrayWithArray:self.picArray];
        [arr removeObjectAtIndex:indexPath.row];
        self.picArray = arr;
        //    [self.picArray ];
        [_collectionView reloadData];
    }
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
